<template>
  <div class="welcome-page">
    <vue-particles
      color="#ffffff"
      :particleOpacity="0.7"
      :particlesNumber="50"
      shapeType="circle"
      :particleSize="4"
      linesColor="#dedede"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="2"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
    >
    </vue-particles>

    <!-- 表单 -->
    <div class="form">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
        <div
          style="
              text-align: center;
              height: 60px;
              line-height: 60px;
              font-weight: bold;
              font-size: 24px;
              color: #fff;
            "
        >
          家政预约服务云平台
        </div>
        <el-form-item prop="username" style="text-align: center">
          <el-input v-model="loginForm.username" placeholder="Username" type="text" ref="username" style="width: 250px" />
        </el-form-item>
        <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="password" style="text-align: center">
            <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="Password" style="width: 250px" />
          </el-form-item>
        </el-tooltip>
        <div style="text-align: center">
          <el-button
            :loading="loading"
            style="width: 250px 
              border: 2px solid #fff;
              color:#fff;
              font-size:18px;
              background-color: transparent;"
            @click.native.prevent="handleLogin"
            >登录</el-button
          >
        </div>
      </el-form>
      <!-- 表单 -->
    </div>
  </div>
</template>

<script>
import { post } from '@/utils/request'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      callback()
    }
    const validatePassword = (rule, value, callback) => {
      callback()
    }
    return {
      visible: false,
      form: {},
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        realname: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        username: 'admin1',
        password: '123321'
      },
      loginRules: {
        username: [
          // { required: true, trigger: "blur", validator: validateUsername },
          { required: true, trigger: 'blur' }
        ],
        password: [
          // { required: true, trigger: "blur", validator: validatePassword },
          { required: true, trigger: 'blur' }
        ]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  // mounted() {
  //   if (this.loginForm.username === "") {
  //     this.$refs.username.focus();
  //   } else if (this.loginForm.password === "") {
  //     this.$refs.password.focus();
  //   }
  // },
  methods: {
    // 提交注册
    submitHandler() {
      this.$refs['registerForm'].validate(valid => {
        if (valid) {
          let url = '/user/register'
          post(url, this.form).then(response => {
            this.$message({ type: 'success', message: response.message })
            this.visible = false
          })
        } else {
          return false
        }
      })
    },
    // 重置表单校验
    clearValidate() {
      this.$refs['registerForm'].clearValidate()
    },
    // 去保存
    toRegisterHandler() {
      this.form = {}
      this.visible = true
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery
              })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style>
.welcome-page {
  /* background: linear-gradient(-180deg, #dcf2e6 0%, #ffffff 100%); */
  background-image: url('../../assets/首页/login-bg.jpg');
  width: 100%;
  height: 100%; /**宽高100%是为了图片铺满屏幕 */
  overflow: hidden;
  position: relative;
}

.form {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  box-sizing: border-box;
  margin: auto;
  background-color: transparent;
  width: 400px;
  height: 300px;
  border-radius: 5px;
  padding: 2em;
  box-shadow: 0 0 10px #999;
  border: 2px solid #fff;
}
</style>
